import user from '../../../utils/user'
import { createDayProject } from '../../../api/app'
import { getInsuranceEnterprise, getInsurePlan } from '../../api/insurance'
const app = getApp()
Page({

    /**
   * 页面的初始数据
   */
    data: {
        enter: '',
        enterId: '',
        insureType: [],
        tip: {
            index: false,
            class: 'left',
            content: '123123312',
            type: ''
        },
        selectIndex: false,
        tipList: false,
        companyList: [],
        isCarryPopup: 0 // 只有在他等于1的时候提示弹窗才会显示
    },

    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function () {
        user.waitLogin().then(()=>{
            getInsuranceEnterprise({
                insureCompanyType: 'insure_type_tyrb'
            }).then((res) => {
                this.setData({
                    'companyList': res
                })
            })
        })
    },
    onEnterInput(event) {
        console.log(event, 'event')
        const data = []
        for (const i in this.data.companyList) {
            console.log(i, this.data.companyList[i])
            // if (this.data.companyList[i].serveCompanyName && this.data.companyList[i].serveCompanyName.indexOf(event.detail.value) !== -1) {
                
            // }
            data.push(this.data.companyList[i])
        }
        console.log(this.data.enter, 'datadata')
        this.setData({
            'enter': this.data.enter,
            'enterId': this.data.enterId,
            tipList: !this.data.tipList ? data : !this.data.tipList,
            
        }, ()=>{
            console.log(this.data.enter, '123tipList')
        })
    },
    // 投保企业管理
    onGoManage() {
        wx.navigateTo({
            url: '/packageMicroInsurance/pages/insurance-organize/insurance-organize',
        })
    },
    onSelect(event) {
        const data = event.currentTarget.dataset
        console.log(data)
        this.setData({
            tip: {
                index: data.index,
                class: data.index % 2 ? 'right' : 'left'
            },
            selectIndex: data.index
        })
    },
    tapEnter(event) {
        if (event.currentTarget.dataset.isdisabled) {
            wx.showToast({
                title: '已禁用',
                icon: 'none'
            })
            return
        }
        setTimeout(()=>{
            this.setData({
                insureType: [],
                'enter': event.currentTarget.dataset.name,
                'enterId': event.currentTarget.dataset.id,
                tipList: false,
                isCarryPopup: this.data.isCarryPopup += 1
            }, ()=>{
                getInsurePlan({
                    serveCompanyId: this.data.enterId,
                    productType: 1
                }).then(res=>{
                    this.setData({
                        insureType: res.map(item=>{
                            item.dictLabel = item.industryPlanName.replace('万', '万 ')
                            return item
                        }),
                    })
                })
                console.log(this.data.enter, this.data.enterId, 'enter')
                this.setData({
                    tip: {
                        class: ''
                    },
                })
            })
        }, 16)
    },
    onSubmit() {
        if(!this.data.enterId) {
            app.toast('请选择投保企业')
            return 
        }
        if(this.data.selectIndex === false) {
            app.toast('请选择投保方案')
            return 
        }

        createDayProject({
            type: 6,
            wesureTypeName: this.data.insureType[this.data.selectIndex].industryPlanName,
            wesureType: this.data.insureType[this.data.selectIndex].wesurePlanCode,
            serveCompanyName: this.data.enter,
            serveCompanyId: this.data.enterId,
            industryPlanId: this.data.insureType[this.data.selectIndex].id
        }).then(res=>{
            app.toast('创建项目成功，正在为您跳转')
            setTimeout(() => {
                wx.redirectTo({
                    url: '/packageMicroInsurance/pages/insurance-insure-detail/insurance-insure-detail?id=' + res
                })
            }, 2000)
        })
    },
    onCarryPopup() {
        this.setData({
            isCarryPopup: 2
        })
    }
})